<?php
class WbController extends DomController {
	
	public function index() {
		
	}
	
	public function wbPageBasic( $id ) {
		$page = WbPageBasic::load( $id );
		$this->view->addPartitioner('contents',$page->getTemplatePath('basicPage.html'));
		$this->view->page = $page;
		
		$pager = new DomPager(0, $page->galleryImagesPerPage, $this->getSiteUrl('/'.$page->getPageUrlKey()) );
		$iterator = $page->galleryImages->query()->countFoundRows()->limit( $pager->getLimit())->commit();
		$pager->setTotal( $iterator->foundRows() );
		$this->view->pages = $pager->getPages();
		$this->view->images = $iterator->toArray();
		
		$this->view->metaAuthor = '';
		$this->view->metaKeywords = $page->metaKeywords;
		$this->view->metaDescription = $page->metaDescription;
		$this->view->pageTitle = $page->metaTitle ? $page->metaTitle : $page->title;
		
	}
	
	public function wbPageFeedback( $id ) {
		$page = WbPageBasic::load( $id );
		$this->view->addPartitioner('contents',$page->getTemplatePath('feedback.html'));
		$this->view->page = $page;
		
		if( $_POST['send'] == 1 ) {
			
			if( $_POST['email'] != $_POST['emailAgain'] ) {
				$this->view->errorMessage = 'Emailid ei ole samad';
			
			} else {
				$mail  = new Mail();
				$mail->CharSet = 'utf-8';
				
				$mail->AddAddress( $page->feedbackEmail );  
				$mail->Subject =  'Tagasiside - '.$_POST['title'];
				$mail->Body = $_POST['contents'];
				$mail->From = $_POST['email'];
				$mail->FromName = $_POST['email'];
				
				if( $mail->Send() ) {
					$this->view->okMessage = 'Teade saadetud';
				} else {
					$this->view->errorMessage = 'Teate saatmine ebaõnnestus';
				}
			}
		}
		
		$this->view->metaAuthor = '';
		$this->view->metaKeywords = $page->metaKeywords;
		$this->view->metaDescription = $page->metaDescription;
		$this->view->pageTitle = $page->metaTitle ? $page->metaTitle : $page->title;
	
	}
	
	public function wbPageNews( $id, $entryId = NULL ) {
		$page = WbPageNews::load( $id );
		$this->view->page = $page;
		if( $entryId ) {
			$entry = $page->entries->find( $entryId );
		}
		
		if( $entry ) {
			$this->view->addPartitioner('contents',$page->getTemplatePath('newsEntryPage.html') );
			$this->view->entry = $entry;
			
		} else {
			$this->view->addPartitioner('contents',$page->getTemplatePath('newsPage.html') );
			$this->view->news = $page->entries->query()->orderBy('t.datetime DESC')->commit()->toArray();	
		}
	}
	
}